package com.wss.lsl.alading.controller.card;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wss.lsl.alading.domain.Card;
import com.wss.lsl.alading.domain.Company;
import com.wss.lsl.alading.service.ICompanyCardService;
import com.wss.lsl.alading.service.IUnitService;
import com.wss.lsl.alading.vo.CardVo;

@Controller
@RequestMapping("/card")
public class CardController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ICompanyCardService companyCardService;
	@Autowired
	private IUnitService unitService;

	@RequestMapping("/show/{id}")
	public String show(@PathVariable("id") String id, Model model) {

		logger.info("show id={}", id);

		model.addAttribute("unit", unitService.findUnit());
		if (id == null || "null".equals(id)) {

			return "card/cardShow";
		} else {
			model.addAttribute("card",
					companyCardService.findCardVoByCardId(Integer.valueOf(id)));
			return "card/cardUpdate";
		}
	}

	@RequestMapping("/create")
	@ResponseBody
	public String create(@ModelAttribute Card card, String companyId, HttpServletResponse response) {

		int type = 3; // 保存成功
		try {

			companyCardService.createOrUpdateCard(card, companyId);
		} catch (Exception e) {
			type = 4;
		}
		
		return "{\"type\":" + type + "}";
	}

	public String update() {

		return "";
	}

	@RequestMapping("/listCompanies")
	@ResponseBody
	public Map<String, Object> listCompanies(Integer page, Integer rows,
			@ModelAttribute Company company) {

		page = (page == null) ? 1 : page;
		rows = (rows == null) ? 10 : rows;

		logger.info(
				"list Companies: page={}, rows={}, companyCode={}, companyName={}",
				new Object[] { page, rows, company.getCode(), company.getName() });

		List<Company> list = companyCardService.searchCompanys(page, rows,
				company);
		Integer count = companyCardService.countCompanys(page, rows, company);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("page", page);
		result.put("rows", list == null ? new ArrayList<Company>() : list);
		result.put("total", count == null ? 0 : count);

		return result;
	}

	@RequestMapping("/checkCardName")
	@ResponseBody
	public String checkCardName(String cardName) {

		int type = 0; // 卡名称不存在
		List<Card> list = companyCardService.findCardsByName(cardName);

		if (null != list && list.size() > 0) {
			type = 7; // 卡名称存在
		}

		return "{\"type\":" + type + "}";
	}

	@RequestMapping("/checkDefaultCard")
	@ResponseBody
	public String checkDefaultCard() {

		int type = 0; // 不存在
		if (null != companyCardService.findDefaultCard()) {
			type = 5; // 默认卡已存在
		}

		return "{\"type\":" + type + "}";
	}

	@RequestMapping("/list0")
	public String list0() {

		return "card/listCard";
	}

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(Integer page, Integer rows, @ModelAttribute CardVo cardvo) {
		
		page = (page == null) ? 1 : page;
		rows = (rows == null) ? 10 : rows;

		logger.info(
				"list cards: page={}, rows={}, cardName={}, defaultCard={}",
				new Object[] { page, rows, cardvo.getCardName(), cardvo.getDefaultCard() });


		List<CardVo> list = companyCardService.findCards(page, rows, cardvo);
		Integer count = companyCardService.countCards(page, rows, cardvo);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("page", page);
		result.put("total", count == null ? 0 : count);
		result.put("rows", list == null ? new ArrayList<CardVo>() : list);
		
		return result;
	}
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") Integer id){
	
		logger.info("delete card: id={}", id);
		
		int type = 3; // delete success
		try {
			companyCardService.removeCard(id);
		} catch (Exception e) {
			type = 4; // delete failure
		}
		
		return "{\"type\":" + type + "}";
	}
}
